package com.night.card.until;

import com.night.common.entity.card.CardIn;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @ClassName:EmailUntil
 * @Description: //邮箱通知服务
 * @author: CHX42
 * @date: 2022/10/28 16:07
 * @version: v1.0
 */
public class EmailUntil {
    public static void sendMail(CardIn cardIn) {
        try {
            final Properties props = new Properties();
            props.put("mail.user", "2232387395@qq.com");   //发件方的邮箱
            props.put("mail.password", "vmhrrlrgyownecgh"); // 授权码（密码）
            props.put("mail.smtp.auth", "true");
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", "smtp.qq.com");
            props.put("mail.smtp.port", "25");

            Session mailSession = Session.getDefaultInstance(props);

            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress("2232387395@qq.com")); // 发件人
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(cardIn.getCard().getStudent().getEmail())); // 收件人
            msg.setSubject("校园卡充值失败-蜗牛学院"); // 主题
            msg.setContent(cardIn.getCard().getStudent().getName()+", 你好，你在"+cardIn.getInTime()+"充值的<span style='color:red;' >"+cardIn.getInMoney()+"</span>元，由于网络波动原因充值失败，充值金额已经原路返回，请进入校园app重新充值！",
                    "text/html;charset=UTF-8");
            msg.saveChanges();

            Transport transport = mailSession.getTransport("smtp");
            transport.connect(props.getProperty("mail.smtp.host"), props.getProperty("mail.user"),
                    props.getProperty("mail.password"));
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
